<?php
/**
 * Minhlog 后台搜索引擎控制器
 *
 * LICENSE
 *
 * This source file is subject to the new BSD license that is bundled
 * with this package in the file LICENSE.txt.
 *
 * @category   Controller
 * @package    Backend
 * @copyright  Copyright (c) 2009-2010 Minh.Shu@gmail.com
 * @license    New BSD License
 * @author     Minh.Shu
 * @version    $Id: SearchController.php 23 2010-02-16 09:03:56Z Minh.Shu $
 *
 */

class Backend_SearchController extends Minh_Controller_Action
{
    protected $_index = null;
    protected $_articleModel;

    public function init()
    {
        if (!isset($_SESSION['Minhlog']['storage']->user_id)) {
            $this->_redirect('/backend/user/login/');
        }

        if(null === $this->_articleModel){
            if(class_exists('Domain_Model_Article')){
                $this->_articleModel = new Domain_Model_Article();
            } else {
                throw new Exception('文章模型类不存在!');
            }
        }

        /**
         * @todo 搜索开关:Lucene/Sphinx
         */
        if(null === $this->_index){
            $this->_index = new Minh_Search_Lucene();
        }

        parent::init();
    }

    /**
     * 建立索引
     */
    public function buildAction()
    {
        if(function_exists('set_time_limit')) set_time_limit(0);

        $articleSet = $this->_articleModel->getAllArticle();
        $this->_index->build();

        foreach($articleSet as $article) {
            $document = array(
                    array(
                            'type'  => 'UnIndexed',
                            'key'   => 'articleId',
                            'value' => (int) $article->article->articleId
                    ),
                    array(
                            'type'  => 'UnIndexed',
                            'key'   => 'categoryId',
                            'value' => (int) $article->article->categoryId
                    ),
                    array(
                            'type'  => 'UnIndexed',
                            'key'   => 'categoryName',
                            'value' => trim($article->category->categoryName)
                    ),
                    array(
                            'type'  => 'Text',
                            'key'   => 'articleTitle',
                            'value' => trim(strip_tags($article->article->articleTitle))
                    ),
                    array(
                            'type'  => 'Text',
                            'key'   => 'articleSummary',
                            'value' => trim(strip_tags($article->article->articleSummary))
                    ),
                    array(
                            'type'  => 'UnStored',
                            'key'   => 'articleContent',
                            'value' => trim(strip_tags($article->article->articleContent))
                    ),
                    array(
                            'type'  => 'UnIndexed',
                            'key'   => 'publishTime',
                            'value' => (int) $article->article->publishTime
                    ),
                    array(
                            'type'  => 'UnIndexed',
                            'key'   => 'clickCount',
                            'value' => (int) $article->article->clickCount
                    ),
                    array(
                            'type'  => 'UnIndexed',
                            'key'   => 'commentCount',
                            'value' => (int) $article->article->commentCount
                    )
            );
            $tagIds = array();
            $tags = array();
            foreach ($article->tags as $key => $tag){
                $tagIds[] = (int) $tag->tagId;
                $tags[] = trim($tag->tagName);
            }
            $tagId = implode(',',$tagIds);
            $tag = implode(',',$tags);
            $document[] = array(
                    'type'  => 'UnIndexed',
                    'key'   => 'tagId',
                    'value' => $tagId
            );
            $document[] = array(
                    'type'  => 'Text',
                    'key'   => 'tag',
                    'value' => $tag
            );
            $this->_index->addDoc($document);
            unset($document);
        }
        $this->_index->optimize();
        $this->_index->commit();

        $this->_redirect('/backend/index/index/');
    }
}